package xyz.zhouzhousag.server.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import xyz.zhouzhousag.server.dao.AdminUserDao;
import xyz.zhouzhousag.server.pojo.entity.AdminUser;
import xyz.zhouzhousag.server.service.AdminUserService;

import javax.annotation.Resource;

/**
 * 管理员表(AdminUser)表服务实现类
 *
 * @author Yin.Liu8
 * @since 2021-09-26 16:21:05
 */
@Service("adminUserService")
public class AdminUserServiceImpl implements AdminUserService {
    @Resource
    private AdminUserDao adminUserDao;

    /**
     * 根据用户名获取后台数据
     *
     * @param username
     * @return
     */
    @Override
    public AdminUser getByName(String username) {
        return adminUserDao.selectByName(username);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param adminUserId 主键
     * @return 实例对象
     */
    @Override
    public AdminUser queryById(Long adminUserId) {
        return this.adminUserDao.queryById(adminUserId);
    }

    /**
     * 分页查询
     *
     * @param adminUser   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<AdminUser> queryByPage(AdminUser adminUser, PageRequest pageRequest) {
        long total = this.adminUserDao.count(adminUser);
        return new PageImpl<>(this.adminUserDao.queryAllByLimit(adminUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param adminUser 实例对象
     * @return 实例对象
     */
    @Override
    public AdminUser insert(AdminUser adminUser) {
        this.adminUserDao.insert(adminUser);
        return adminUser;
    }

    /**
     * 修改数据
     *
     * @param adminUser 实例对象
     * @return 实例对象
     */
    @Override
    public AdminUser update(AdminUser adminUser) {
        this.adminUserDao.update(adminUser);
        return this.queryById(adminUser.getAdminUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param adminUserId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long adminUserId) {
        return this.adminUserDao.deleteById(adminUserId) > 0;
    }
}
